﻿using System;
using System.Collections.ObjectModel;

using Sunyi.Project.WebPoint.DataContract;
using Sunyi.Project.WebPoint.ServiceAccess;

namespace Sunyi.Project.WebPoint.BusinessLogic
{
    public partial class UserManager
    {
        public User SelectByName(string userName)
        {
            return controller.SelectByName(userName);
        }

        public void Login(string userName)
        {
            // Grant default roles for current user.
            var userManager = new UserManager();
            var user = userManager.SelectByName(userName);

            if (user != null)
            {
                bool flag = false;
                foreach (var role in RoleManager.DefaultRoles)
                {
                    if (!user.Roles.Contains(role)) { user.Roles.Add(role); flag = true; }
                }
                if (flag) userManager.Save(user);
            }
        }

        public void UploadUserInfo(User user, Collection<Guid> removedRoles)
        {
            var userServiceProxy = new UserServiceProxy();
            userServiceProxy.UpdateUser(user, removedRoles);
        }

        public int SearchCount(string userName, string roleName, bool isActive)
        {
            return controller.SearchCount(userName, roleName, isActive);
        }

        public Collection<User> Search(string userName, string roleName, bool isActive, int pageIndex, int pageSize)
        {
            // No need cache data for this method.
            return controller.Search(userName, roleName, isActive, pageIndex, pageSize);
        }
    }
}
